//
//  YuleViewController.m
//  New
//
//  Created by 才诗琦 on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com. All rights reserved.
//
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

#import "YuleViewController.h"

@interface YuleViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, assign)NSInteger number;
@end

@implementation YuleViewController
-(void)dealloc
{
    [super dealloc];
    [_tableView dealloc];
    
}


-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //创建tableView
        [self createTableView];
//        self.number = 0;
        
    }    return self;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   
}

#warning 上拉加载
- (void)loadnewdata
{
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1348648517839/%ld-20.html", self.number]  completion:^(id result) {
        NSArray *arrTemp = [NSArray array];
        arrTemp = [YuleModel modelHandlerWithArray:result[@"T1348648517839"]];
        for (NSInteger i = 0; i < 20; i++) {
            [self.dataArr addObject:arrTemp[i]];
        }
        [_tableView reloadData];
    }];
    [self.tableView.mj_footer endRefreshing];
    self.number += 20;
}

#warning 下拉刷新
- (void)dadaHandeler
{
    [AFNModel GETWithHandler: @"http://c.3g.163.com/nc/article/list/T1348648517839/0-20.html"  completion:^(id result) {
        self.dataArr = [NSMutableArray array];
               self.dataArr = [YuleModel modelHandlerWithArray:result[@"T1348648517839"]];
           dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableView reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.mj_header endRefreshing];
    });
    
}];

}

#warning 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT-150 ) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
   //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dadaHandeler)];
    [self.tableView.mj_header beginRefreshing];
    [self.tableView.mj_header endRefreshing];
    
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadnewdata)];
    if (_dataArr.count != 0) {
        [self.tableView.mj_footer beginRefreshing];
    }
    self.number = 20;
    
       //注册cell
    //页面首条数据
    [_tableView registerClass:[YuleCell class] forCellReuseIdentifier:@"cell"];
    
    //一张图片的cell
    [_tableView registerClass:[YuleCell2 class] forCellReuseIdentifier:@"cell2"];
    //三张图片的cell
    [_tableView registerClass:[YuleCell3 class] forCellReuseIdentifier:@"cell3"];
    
    
    //添加父视图
    [self.view addSubview:_tableView];
    //管理内存
   [_tableView release];
    
    
}

#warning 处理数据 方法实现
- (UITableView *)setWithUrlStr:(NSString *)str
{
//    
    [AFNModel GETWithHandler:str completion:^(id result) {
        
       self.dataArr = [YuleModel modelHandlerWithArray:result[@"T1348648517839"]];
       
        [_tableView reloadData];
        
        
    }];
    

 
    return _tableView;
}
//tableView的协议方法实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _dataArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    YuleModel *yule =_dataArr[indexPath.row];
   //判断
    if ([yule.hasHead stringValue].length != 0) {
        
    //页面第一条 为第一个cell
    YuleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        [cell.imagView sd_setImageWithURL:[NSURL URLWithString:yule.imgsrc]];
        cell.titleLabel.text = yule.title;
        
        return cell;
        
    }
    //显示三张图片的cell 为第二个cell
        if (yule.imgextra != NULL  && [yule.hasHead stringValue].length == 0) {
            
            YuleCell3 *cell3 = [tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
          [cell3.imagView sd_setImageWithURL:[NSURL URLWithString:yule.imgextra[0][@"imgsrc"]]];
          [cell3.imagView1 sd_setImageWithURL:[NSURL URLWithString:yule.imgextra[1][@"imgsrc"]]];
   
          [cell3.imagView2 sd_setImageWithURL:[NSURL URLWithString:yule.imgsrc]];
            cell3.titleLabel.text = yule.title;
            return cell3;
        }
        
//    显示一张图片 的cell 为第三个cell
    else{
        YuleCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        
        cell2.titleLabel.text = yule.title;
        cell2.contentLabel.text = yule.digest;
        [cell2.imag sd_setImageWithURL:[NSURL URLWithString:yule.imgsrc]];
        cell2.commentCountLabel.text = [[yule.replyCount stringValue] stringByAppendingString:@"跟帖"];
        return cell2;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YuleModel *yule =_dataArr[indexPath.row];
    
    if ([yule.digest isEqualToString:@""]) {
    
    
    //cell2的高度
       if ([yule.hasHead stringValue].length == 0) {
        
        return 190;
    }
        //cell的高度
        return 220;
    }
    else
    {
        //cell3的高度
        CGFloat h = [YuleCell2 heightWithText:yule.digest];
        return h + 70;
        
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YuleModel *yule = _dataArr[indexPath.row];
    [self.delegate pass:yule.url];
    
    
    
    
}

//重写set方法
//- (void)setDataArr:(NSArray *)dataArr
//{
//    if (_dataArr) {
//        [_dataArr release];
//    }
//    _dataArr = [dataArr retain];
//    [_tableView reloadData];
//}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
